#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10;
int n , k;
long long a[N],sl[N],ss[N];
long long s[N];
int main()
{
    cin >> n >> k;
    for(int i = 1; i<= n; i++)cin >> a[i];
    int hh = 0, tt = -1;
    for(int i = 1;i <= n ; i++){
        if(hh <= tt && i -  s[hh] >= k ) hh++;
        while(hh <= tt && a[s[tt]] > a[i]) tt--;
        s[++tt] = i;
       if(i >= k)cout << a[s[hh]] <<" ";
    }

    cout <<"\n";
    
    hh = 0, tt = -1;
    for(int i = 1; i <= n; i++){
        if(hh <= tt && i - s[hh] >= k) hh++;
        while(hh <= tt && a[s[tt]] < a[i])tt--;
        s[++tt] = i;
        if(i >= k)cout << a[s[hh]] <<" ";
    }



   return 0;
}
